import http from 'axios';
import { layer } from "@layui/layer-vue";

// 设置请求基础地址
http.defaults.baseURL = VITE_API_BASE_URL;
http.defaults.timeout = 3000;
// 添加请求拦截器
http.interceptors.request.use(config => {
	// const store = useUserStore();
	// if (store.appSessionId && !config.headers.Authorization) {
	// 	config.headers.Authorization = 'bearer ' + store.appSessionId
	// }

	return config;
}, error => Promise.reject(error));

// 响应拦截
http.interceptors.response.use(({ data: response }) => {
	let msg = response.msg || '网络开小差了';
	// 处理全局的事情
	switch (response.code) {
		case -1:
			layer.msg("网络开小差了", { icon : 5, time: 1000})
			return Promise.reject('网络开小差了');
			break;
		case 401:
			layer.msg(msg, { icon : 5, time: 1000})
			return Promise.reject(msg);
			break;
		case 400:
			msg = response.msg + '，请检查输入是否有问题' || '网络开小差了';
			
			layer.msg(msg, { icon : 5, time: 1000})
			return Promise.reject(msg);
			break;
		case 200:
			return response;
			break;
		case 405:
			msg = response.msg || '网络开小差了';
			layer.msg(msg, { icon : 5, time: 1000})
			return response;
			break;
		case 421:
			msg = response.msg || '网络开小差了';
			layer.msg(msg, { icon : 5, time: 1000})
			return response;
			break;
	}
}, error => Promise.reject(error));
export default http;